package com.gt.wxpay.cashier.payChannel.application.impl;

import com.gt.wxpay.cashier.domain.dto.QueryRefundDto;
import com.gt.wxpay.cashier.domain.model.CashierPayment;
import com.gt.wxpay.cashier.payChannel.application.WxPayService;
import com.gt.wxpay.cashier.payChannel.callback.vo.WxPayRespDto;
import com.gt.wxpay.cashier.payChannel.handler.OperationWxHandler;
import com.wechat.pay.java.core.RSAAutoCertificateConfig;
import com.wechat.pay.java.service.payments.jsapi.JsapiService;
import com.wechat.pay.java.service.payments.jsapi.model.QueryOrderByIdRequest;
import com.wechat.pay.java.service.payments.model.Transaction;
import com.wechat.pay.java.service.refund.model.Refund;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * packageName com.gt.wxpay.cashier.payChannel.application.impl
 *
 * @author GuoTong
 * @version JDK 8
 * @className WxConfigService
 * @date 2024/3/16
 * @description 初始化系统配置微信
 */
@Slf4j
@Service
public class WxPaySerivceImpl implements WxPayService {


    @Autowired
    private OperationWxHandler operationWxHandler;

    /**
     * 微信支付的下单
     *
     * @param payment CashierPayment
     * @return WxPayRespDto
     */
    @Override
    public WxPayRespDto unifiedOrderV3(CashierPayment payment) {
        // 获取指定的渠道信息
        WxPayRespDto wxPayRespDto;
        try {
            // 下单分流--执行各种方式的下单操作
            wxPayRespDto = operationWxHandler.handlePrepay(payment);
        } catch (Exception e) {
            log.error("发起支付出错: {}", e.getMessage(), e);
            throw new RuntimeException("发起支付出错: " + e.getMessage());
        }
        return wxPayRespDto;
    }


    /**
     * 关闭订单，以下情况需要调用关单接口：
     * <p>
     * 商户订单支付失败需要生成新单号重新发起支付，要对原订单号调用关单，避免重复支付；
     * 系统下单后，用户支付超时，系统退出不再受理，避免用户继续，请调用关单接口。
     *
     * @param cashierPayment cashierPayment
     */

    @Override
    public void closePayment(CashierPayment cashierPayment) {
        log.info("关闭支付订单收银台流水：" + cashierPayment.getPaymentNo());
        operationWxHandler.closePayment(cashierPayment);
    }

    /**
     * 微信回调成功:发送MQ消息成功
     *
     * @param transaction Transaction
     */
    private void sendMqSuccessPayCallBackMsg(Transaction transaction) {
    }



    /**
     * 退款
     *
     * @param cashierPayment cashierPayment
     * @return Refund
     */
    @Override
    public Refund refundV3(CashierPayment cashierPayment) {
        log.info("执行退款业务处理-------");
        return operationWxHandler.refundOrderV3(cashierPayment);
    }




    @Override
    public Transaction queryOrderV3(String mchId, String transId) {
        RSAAutoCertificateConfig config = new RSAAutoCertificateConfig.Builder()
                .build();
        JsapiService service = new JsapiService.Builder().config(config).build();
        QueryOrderByIdRequest request = new QueryOrderByIdRequest();
        request.setMchid(mchId);
        request.setTransactionId(transId);
        return service.queryOrderById(request);
    }

    /**
     * 查询接口
     *
     * @param payment 支付单
     * @return WxPayRespDto
     */
    @Override
    public CashierPayment queryOrderInfo(CashierPayment payment) {
        CashierPayment wxPayRespDto;
        try {
            // 下单分流--执行各种方式的下单操作（查询）
            wxPayRespDto = operationWxHandler.queryOrderInfo(payment);
        } catch (Exception e) {
            log.error("查询支付订单出错: {}", e.getMessage(), e);
            throw new RuntimeException("查询支付订单出错: " + e.getMessage());
        }
        return wxPayRespDto;
    }

    @Override
    public QueryRefundDto queryRefund(CashierPayment refundOrder) {
        QueryRefundDto queryRefundDto;
        try {
            // 下单分流--执行各种方式的下单操作（查询）
            queryRefundDto = operationWxHandler.queryRefund(refundOrder);
        } catch (Exception e) {
            log.error("查询退款订单出错: {}", e.getMessage(), e);
            throw new RuntimeException("查询退款订单出错: " + e.getMessage());
        }
        return queryRefundDto;
    }

}
